package ch11;

import java.util.Stack;

public class _08_StackEx {
	public static Stack<Integer> back    = new Stack<Integer>();
	public static Stack<Integer> forward = new Stack<Integer>();  

	public static void main(String[] args) {
		goURL(1);
		goURL(2);
		goURL(3);
		goURL(4);
		printStatus();
		/*
		back: [1, 2, 3, 4]
		forward: []
		now: 4
		*/

		goBack();
		System.out.println("click back.");  
		printStatus();
		/*
		click back.
		back: [1, 2, 3]
		forward: [4]
		now: 3
		*/

		goBack();
		System.out.println("click back.");  
		printStatus();
		/*
		click back.
		back: [1, 2]
		forward: [4, 3]
		now: 2
		*/

		goForward();
		System.out.println("click forward.");  
		printStatus();
		/*
		click forward.
		back: [1, 2, 3]
		forward: [4]
		now: 3
		*/

		goURL(5);
		System.out.println("go to 5.");  
		printStatus();
		/*
		go to 5.
		back: [1, 2, 3, 5]
		forward: []
		now: 5
		*/
	}

	public static void printStatus() {
		System.out.println("back: "+back);
		System.out.println("forward: "+forward);
		System.out.println("now: " + back.peek());  
		System.out.println();
	}

	public static void goURL(int url){
		back.push(url);
		if(!forward.empty()) forward.clear();
	}

	public static void goForward(){
		if(!forward.empty())
		back.push(forward.pop());
	}

	public static void goBack(){
		if(!back.empty())
		forward.push(back.pop());
	}
}
